package com.ruoyi.plasma.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.tongji.utils.DateUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.plasma.domain.PlasmaUncollectionStatistics;
import com.ruoyi.plasma.service.IPlasmaUncollectionStatisticsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 未采人员登记Controller
 * 
 * @author ruoyi
 * @date 2024-11-18
 */
@RestController
@RequestMapping("/plasma/unCollection")
public class PlasmaUncollectionStatisticsController extends BaseController
{
    @Autowired
    private IPlasmaUncollectionStatisticsService plasmaUncollectionStatisticsService;

    /**
     * 查询未采人员登记列表
     */
    @PreAuthorize("@ss.hasPermi('plasma:unCollection:list')")
    @GetMapping("/list")
    public TableDataInfo list(PlasmaUncollectionStatistics plasmaUncollectionStatistics)
    {
        startPage();
        List<PlasmaUncollectionStatistics> list = plasmaUncollectionStatisticsService.selectPlasmaUncollectionStatisticsList(plasmaUncollectionStatistics);
        return getDataTable(list);
    }

    /**
     * 导出未采人员登记列表
     */
    @PreAuthorize("@ss.hasPermi('plasma:unCollection:export')")
    @Log(title = "未采人员登记", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PlasmaUncollectionStatistics plasmaUncollectionStatistics)
    {
        List<PlasmaUncollectionStatistics> list = plasmaUncollectionStatisticsService.selectPlasmaUncollectionStatisticsList(plasmaUncollectionStatistics);
        ExcelUtil<PlasmaUncollectionStatistics> util = new ExcelUtil<PlasmaUncollectionStatistics>(PlasmaUncollectionStatistics.class);
        util.exportExcel(response, list, "未采人员登记数据");
    }

    /**
     * 获取未采人员登记详细信息
     */
    @PreAuthorize("@ss.hasPermi('plasma:unCollection:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(plasmaUncollectionStatisticsService.selectPlasmaUncollectionStatisticsById(id));
    }

    /**
     * 新增未采人员登记
     */
    @PreAuthorize("@ss.hasPermi('plasma:unCollection:add')")
    @Log(title = "未采人员登记", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PlasmaUncollectionStatistics plasmaUncollectionStatistics)
    {
        return toAjax(plasmaUncollectionStatisticsService.insertPlasmaUncollectionStatistics(plasmaUncollectionStatistics));
    }

    /**
     * 修改未采人员登记
     */
    @PreAuthorize("@ss.hasPermi('plasma:unCollection:edit')")
    @Log(title = "未采人员登记", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PlasmaUncollectionStatistics plasmaUncollectionStatistics)
    {
        return toAjax(plasmaUncollectionStatisticsService.updatePlasmaUncollectionStatistics(plasmaUncollectionStatistics));
    }

    @PreAuthorize("@ss.hasPermi('plasma:statistics:day:detail')")
    @Log(title = "保存未采人员信息", businessType = BusinessType.UPDATE)
    @PostMapping("/saveUnCollection")
    public AjaxResult saveUnCollection(@RequestBody HashMap hashMap)
    {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        String userName = user.getUserName();
        String riqi = DateUtil.format(new Date(),"yyyy-MM-dd");
        Date 日期 = DateUtil.getDate(riqi);
        PlasmaUncollectionStatistics pu = new PlasmaUncollectionStatistics();

        //判断这个过号未采的是否已记录
        pu.set日期(日期);
        pu.setProcessNo((Integer)hashMap.get("processno"));
        List<PlasmaUncollectionStatistics> l = plasmaUncollectionStatisticsService.selectPlasmaUncollectionStatisticsList(pu);
        if (l.size() != 0) {
            pu = l.get(0);
        }
        pu.setDonorNo((String)hashMap.get("donorno"));
        pu.setDonorId((String)hashMap.get("donorid"));
        pu.setDonorName((String)hashMap.get("donorname"));
        pu.setReason((String)hashMap.get("reason"));
        pu.setIntervalTime(hashMap.get("intervalTime") != null ? hashMap.get("intervalTime").toString() : null);
        pu.setDonorState((String)hashMap.get("donorstate"));
        pu.setProcessNo((Integer)hashMap.get("processno"));
        pu.setPhone((String)hashMap.get("phone"));
        pu.setType((String)hashMap.get("type"));
        pu.setDonorState((String)hashMap.get("codedonorstate"));
        pu.setDonorType((String) hashMap.get("changeless"));
        pu.setFollowName(userName);
        pu.setFollowTime(DateUtil.format(new Date(),"yyyyMMddHHmmss"));
        pu.setPlasmaCollection((String)hashMap.get("plasmacollection"));
        //如果已记录过，更新一下记录信息。如果没有记录过，插入一条记录
        if (pu.getId() != null) {
            return toAjax(this.plasmaUncollectionStatisticsService.updatePlasmaUncollectionStatistics(pu));
        } else {
            pu.setInsertTime(DateUtil.format(new Date(),"yyyyMMddHHmmss"));
            return toAjax(this.plasmaUncollectionStatisticsService.insertPlasmaUncollectionStatistics(pu));
        }
    }

    /**
     * 删除未采人员登记
     */
    @PreAuthorize("@ss.hasPermi('plasma:unCollection:remove')")
    @Log(title = "未采人员登记", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(plasmaUncollectionStatisticsService.deletePlasmaUncollectionStatisticsByIds(ids));
    }
}
